package com.hqw.mindanews;

import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

import com.hqw.mindanews.adapter.MyNewsAdapter;
import com.hqw.mindanews.application.SysApplication;
import com.hqw.mindanews.bean.NewsMsg;
import com.hqw.mindanews.log.Log;
import com.hqw.mindanews.view.CustomProgressDialog;
import com.hqw.mindanews.view.RoundAngleImageView;
import com.hqw.mindanews.view.XListView;
import com.hqw.mindanews.view.XListView.IXListViewListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MyNewsActivity extends Activity implements IXListViewListener, OnClickListener {
	private XListView mNewsListView;
	private RoundAngleImageView mTopHead;
	private TextView mCancelEdit;
	private TextView mTitle;
	private BmobUser currentUser;
	private MyNewsAdapter mAdapter;
	private CustomProgressDialog progressDialog = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page2);
		initView();
		startProgressDialog();
		SysApplication.getInstance().addActivity(this);
	}


	private void initView() {
		 mNewsListView = (XListView) findViewById(R.id.mynews_pull_to_refresh_xListView);
		 mNewsListView.setPullLoadEnable(true);// 是否允许加载更多
		 mNewsListView.setXListViewListener(this);
		 mTopHead = (RoundAngleImageView) findViewById(R.id.top_head);
		 mTopHead.setVisibility(View.GONE);
		 mCancelEdit = (TextView) findViewById(R.id.cancel_edit);
		 mCancelEdit.setVisibility(View.VISIBLE);
		 mCancelEdit.setText("《返回");
		 mCancelEdit.setOnClickListener(this);
		 mTitle = (TextView) findViewById(R.id.title);
		 mTitle.setText("我的投稿");
		 currentUser = BmobUser.getCurrentUser(this);
		 getMyNewsData();
	}

	private void getMyNewsData() {
		BmobQuery<NewsMsg> query = new BmobQuery<NewsMsg>();
		query.addWhereEqualTo("user", currentUser);
		query.order("-createdAt");
		query.findObjects(this, new FindListener<NewsMsg>() {
			   
			@Override
			public void onSuccess(List<NewsMsg> NewsMsg) {
				Log.i("bmob",NewsMsg);
				mAdapter = new MyNewsAdapter(MyNewsActivity.this, NewsMsg);
				mNewsListView.setAdapter(mAdapter);
				stopProgressDialog();
				//mNewsListView.stopRefresh();
				onLoad();
			}
			
			@Override
			public void onError(int arg0, String arg1) {
				
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		 switch (v.getId()) {
		case R.id.cancel_edit:
			finish();
			break;

		default:
			break;
		}

	}
	public void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
		}

		progressDialog.show();
	}

	public void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}
	@Override
	public void onRefresh() {
		 getMyNewsData();
		 //onLoad();
	}

	@Override
	public void onLoadMore() {

	}

	/** 加载完毕 */
	private void onLoad() {
		Log.i("bmob","tingzhi");
		mNewsListView.stopRefresh();
		mNewsListView.stopLoadMore();
		mNewsListView.setRefreshTime(new Date().toString());
	}

	 
 

	 
 

}
